package com.dq.controller.op.opmerchant;

import com.dq.annotation.aop.SysLog;
import com.dq.exception.customException.AdminException;
import com.dq.controller.op.opmerchant.in.query.SpecificationQuery;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.goods.Specification;
import com.dq.service.AllService;
import com.dq.utils.BeanNullUtil;
import com.dq.validator.ValidatorUtils;
import com.dq.validator.group.AddGroup;
import com.dq.validator.group.UpdateGroup;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author gewenzhen
 */
@Api(description = "商品规格管理")
@RestController
@RequestMapping("/specification")
public class SpecificationController extends AllService {
    @ApiOperation("所有商品规格列表")
    @PostMapping("/list")
    @RequiresPermissions("specificationList:list")
    public Vo list(@RequestBody SpecificationQuery specificationQuery) throws AdminException {
        specificationQuery.setSection(shiroService.getDept());
        //查询列表数据
        specificationQuery.format();
        Map<String, Object> query = specificationQuery.parseMap();
        List<Specification> list = dao.find(dao.queryAdminHql(Specification.class, query), query);
        PageHelper.startPage(specificationQuery.getPage(), specificationQuery.getSize());
        PageInfo pageInfo = new PageInfo((List<Specification.Vo>) VO.toVOCollection(list));
        pageInfo.setTotal(dao.count(dao.countAdminHql(Specification.class, query), query));
        return new Vo(pageInfo);
    }

//    @ApiOperation("所有商品规格列表")
//    @PostMapping("/all/{goodId}")
//    @RequiresPermissions("specificationList:all")
//    public Vo all(@PathVariable @ApiParam("商品id") Long goodId) throws AdminException {
//        Goods goods = dao.get(Goods.class, goodId);
//        if (goods == null) {
//            throw new AdminException("商品不存在");
//        }
//        List<Specification> specifications = specificationRepository.findByOrgAndDeletedFalseOrderBySortNoAsc(goods.getOrg());
//        return new Vo(VO.toVOCollection(specifications, false));
//    }

    @ApiOperation("保存规格")
    @PostMapping("/save")
    @RequiresPermissions("specificationList:save")
    public Vo save(@RequestBody Specification specification) throws
            AdminException {
        ValidatorUtils.validateEntity(specification, AddGroup.class);
        specification.setCreateUser(shiroService.getUser());
        specification.setOrg(shiroService.getDept());
        specificationRepository.save(specification);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("修改属性")
    @PostMapping("/update/{specificationId}")
    @RequiresPermissions("specificationList:update")
    public Vo update(@RequestBody Specification updateSpecification, @PathVariable @ApiParam(value = "属性id") Long
            specificationId) throws AdminException {
        ValidatorUtils.validateEntity(updateSpecification, UpdateGroup.class);

        Specification specification = dao.get(Specification.class, specificationId);
        if (specification == null) {
            throw new AdminException("规格不存在");
        }
        specification.setUpdateUser(shiroService.getUser());
        BeanUtils.copyProperties(updateSpecification, specification, BeanNullUtil.getNullPropertyNames(updateSpecification));
        specificationRepository.save(specification);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("删除规格")
    @SysLog("删除规格")
    @PostMapping("/delete/{specificationId}")
    @RequiresPermissions("specificationList:del")
    public Vo delete(@PathVariable @ApiParam(value = "规格id") Long specificationId) throws AdminException {
        Specification specification = dao.get(Specification.class, specificationId);
        if (null == specification) {
            throw new AdminException("规格不存在");
        }
        specification.setDeleted(true);
        specificationRepository.save(specification);
        return new Vo(HttpStatus.OK);
    }
}
